package com.zhangqiancheng.wcdmaconsole;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SysSettingActivity extends Activity {

	public static final String SYS_SETTINGS = "com.zhangqiancheng.wcdmaconsole.SYS_SETTINGS";
	public static final String DEVICE_IP = "com.zhangqiancheng.wcdmaconsole.DEVICE_IP";
	public static final String DEVICE_PORT = "com.zhangqiancheng.wcdmaconsole.DEVICE_PORT";
	public static final String RECEIVE_PORT = "com.zhangqiancheng.wcdmaconsole.RECEIVE_PORT";
	public static final String DEFAULT_DEVICE_IP = "192.168.1.100";
	public static final String DEFAULT_DEVICE_PORT = "5070";
	public static final String DEFAULT_RECEIVE_PORT = "5071";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sys_setting);
		
		SharedPreferences prefs = getSharedPreferences(SYS_SETTINGS, Context.MODE_PRIVATE);
		String deviceIp= prefs.getString(DEVICE_IP, DEFAULT_DEVICE_IP);
		String devicePort=prefs.getString(DEVICE_PORT, DEFAULT_DEVICE_PORT);
		String receivePort=prefs.getString(RECEIVE_PORT, DEFAULT_RECEIVE_PORT);
		
		((EditText) findViewById(R.id.etDeviceIp)).setText(deviceIp);
		((EditText) findViewById(R.id.etDevicePort)).setText(devicePort);
		((EditText) findViewById(R.id.etReceivePort)).setText(receivePort);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sys_setting, menu);
		return true;
	}
	
	public void btnOkOnClick(View view ){
		
		String deviceIP = ((EditText) findViewById(R.id.etDeviceIp)).getText().toString();
		String devicePort = ((EditText) findViewById(R.id.etDevicePort)).getText().toString();
		String receivePort = ((EditText) findViewById(R.id.etReceivePort)).getText().toString();
		
		SharedPreferences prefs = getSharedPreferences(SYS_SETTINGS, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putString(DEVICE_IP, deviceIP);
		editor.putString(DEVICE_PORT, devicePort);
		editor.putString(RECEIVE_PORT, receivePort);
		editor.commit();
		Toast.makeText(this, "系统参数设置成功。", Toast.LENGTH_SHORT).show();
		onBackPressed();
	}

	public void btnBackOnClick(View view){
		onBackPressed();
	}
}
